package com.chy.blog_server.modular.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chy.blog_server.core.util.PageAndParamUtil;
import com.chy.blog_server.modular.web.entity.BoMusic;
import com.chy.blog_server.modular.web.service.IBoMusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：音乐表 controller
 * 作者：chenhuiyan
 * 时间：2020/03/20 22:07
 */
@Controller
@RequestMapping(value = "/web/boMusic")
public class BoMusicController {

    /**
     * 前缀
     */
    private static String PREFIX = "web/boMusic/";

    @Autowired
    private IBoMusicService boMusicService;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "")
    public String index(){
        return PREFIX + "boMusic";
    }

    /**
     * 查询列表
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            Map<String, Object> params = PageAndParamUtil.getParams();
            List<Map<String, Object>> list = boMusicService.list(params); //获取数据列表
            Integer count = boMusicService.getCount(params); //获取总数
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", count);
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 跳转编辑页面
     * @return
     */
    @RequestMapping(value = "/edit")
    public String edit(String id, Model model){
        model.addAttribute("id", id);
        return PREFIX + "boMusic_edit";
    }

    /**
     * 保存
     * @param boMusic
     * @param request
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String, Object> save(BoMusic boMusic, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try {
            boMusic.preUpdate(request);
            boMusicService.updateById(boMusic);
            res.put("state", "200");
            res.put("message", "保存成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "保存失败");
        }
        return res;
    }

    /**
     * 上传音乐页面
     * @return
     */
    @RequestMapping(value = "/add")
    public String add(String id, Model model){
        model.addAttribute("id", id);
        return PREFIX + "boMusic_add";
    }

    /**
     * 上传音乐
     * @param file
     * @param request
     * @return
     */
    @RequestMapping(value = "/uploadMusic")
    @ResponseBody
    public Map<String, Object> uploadMusic(MultipartFile file, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try {
            res = boMusicService.uploadMusic(file, request);
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "服务器异常, 上传失败");
        }

        return res;
    }


    /**
     * 根据id查找信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/getInfoById")
    @ResponseBody
    public Map<String, Object> getInfoById(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            res = boMusicService.getInfoById(id);
            res.put("state", "200");
            res.put("message", "获取成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "获取失败");
        }
        return res;
    }

    /**
     * 删除
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(String id, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            BoMusic boMusic = boMusicService.getById(id);
            boMusic.setDelFlag(1);
            boMusic.preUpdate(request);
            boMusicService.updateById(boMusic);
            res.put("state", "200");
            res.put("message", "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除失败");
        }
        return res;
    }

    /**
     * 修改是否显示状态
     * @param isShow
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateIsShow")
    @ResponseBody
    public Map<String, Object> updateIsShow(@RequestParam(name = "isShow") Integer isShow,
        @RequestParam(name = "musicId") String musicId, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            BoMusic boMusic = boMusicService.getById(musicId);
            boMusic.setIsShow(isShow);
            boMusic.preUpdate(request);
            boMusicService.updateById(boMusic);
            res.put("state", "200");
            res.put("message", "修改成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "修改失败");
        }
        return res;
    }

    /**
     * 回收站首页
     * @return
     */
    @RequestMapping(value = "/recycleIndex")
    public String recycleIndex(){
        return PREFIX + "boMusicRecycle";
    }

    /**
     * 查询已删除列表
     * @return
     */
    @RequestMapping(value = "/recycleList")
    @ResponseBody
    public Map<String, Object> recycleList(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            Map<String, Object> params = PageAndParamUtil.getParams();
            List<Map<String, Object>> list = boMusicService.recycleList(params); //获取数据列表
            Integer count = boMusicService.getBaseMapper().selectCount(new QueryWrapper<BoMusic>().eq("del_flag", 1)); //获取总数
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", count);
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 彻底删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/thoroughlyDelete")
    @ResponseBody
    public Map<String, Object> thoroughlyDelete(String id){
        Map<String, Object> res = new HashMap<>();
        try{
            boMusicService.thoroughlyDelete(id);
            res.put("state", "200");
            res.put("message", "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "删除失败");
        }
        return res;
    }

    /**
     * 恢复
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/restore")
    @ResponseBody
    public Map<String, Object> restore(String id, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            BoMusic boMusic = boMusicService.getById(id);
            boMusic.setDelFlag(0);
            boMusic.preUpdate(request);
            boMusicService.updateById(boMusic);
            res.put("state", "200");
            res.put("message", "恢复成功");
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "恢复失败");
        }
        return res;
    }

}
